if (window.addEventListener)
    window.addEventListener('load', addVortexBMapBaseJs, false);
else if (window.attachEvent)
    window.attachEvent('onload', addVortexBMapBaseJs);
else window.onload = addVortexBMapBaseJs;

function addVortexBMapBaseJs() {
    if(SHOW_MAP == '1'){
        loadJs("http://api.map.baidu.com/api?v=2.0&ak=EVlFc6DZzAzU5avIjoxNcFgQ&callback=addVortexBMapPlugJs");
    }else if(SHOW_MAP == '2'){
        loadJs("http://api.map.baidu.com/api?v=2.0&ak=EVlFc6DZzAzU5avIjoxNcFgQ&callback=addBMapPlugJs");
    }else{
        $('#map').remove();
        loadJs(MAIN_PAGE);
    }
}
function addVortexBMapPlugJs() {
    var urls = [];
    loadJs("http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js");
    loadJs("http://api.map.baidu.com/library/InfoBox/1.2/src/InfoBox_min.js");
    loadJs("http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js");
    loadJs("http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js");
    loadJs("http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js");
    loadJs("http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js");
    loadJs("http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js");
    loadJs("http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.css");
    loadJs("http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.js");
    vtxMap.init();
    
    //vtxMap.init.bind(vtxMap)  这么写是为了将指针指回vtxMap,否则,vtxMap.init内部的指针是指的window
    //在地图加载完后加载react,避免数据获取不到
    loadJs(MAIN_PAGE);
}
function addBMapPlugJs() {
    var urls = [];
    loadJs("http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js");
    loadJs("http://api.map.baidu.com/library/InfoBox/1.2/src/InfoBox_min.js");
    loadJs("http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js");
    loadJs("http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js");
    loadJs("http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js");
    loadJs("http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js");
    loadJs("http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js");
    loadJs("http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.css");
    loadJs("http://api.map.baidu.com/library/TrafficControl/1.4/src/TrafficControl_min.js");
    
    //vtxMap.init.bind(vtxMap)  这么写是为了将指针指回vtxMap,否则,vtxMap.init内部的指针是指的window
    //在地图加载完后加载react,避免数据获取不到
    loadJs(MAIN_PAGE);
} 
//动态加载script和css
function loadJs(loadUrl,callback){
    var loadScript;
    if(loadUrl.substr(loadUrl.length-4) === '.css'){
        loadScript = document.createElement("link");
        loadScript.setAttribute("rel","stylesheet");
        loadScript.setAttribute("href",loadUrl);
    }else{
        loadScript = document.createElement("script");
        loadScript.setAttribute("type","text/javascript");
        loadScript.setAttribute('src',loadUrl);
    }
    document.body.appendChild(loadScript);
    if(!!callback && typeof(callback) === 'function'){
        //判断服务器
        if(navigator.userAgent.indexOf("IE") >=0){
            //IE下的事件
            loadScript.onreadystatechange=function(){
                if(loadScript && (loadScript.readyState == "loaded" || loadScript.readyState == "complete")){
                    //表示加载成功  清空上次的状态监管
                    loadScript.onreadystatechange=null;
                    callback()//执行回调
                }
            }
        }else{
            loadScript.onload=function(){
                //清空上次的状态监管
                loadScript.onload=null;
                callback();
            }
        }
    }
}

function getArg (key) {
    var l = window.location.hash;
    if(l.indexOf('?') == -1)return '';
    l = l.split('?')[1];
    ary = l.split("&");
    for(var i = 0 ; i < ary.length ; i++){
        if(ary[i].split("=")[0]==key){
            return ary[i].split("=")[1];
        }
    }
    return '';
}